package com.gopay.common.security.apicert.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.cust.CustCaCertConstants;
import com.gopay.common.domain.cert.CorpMsgSignCert;
import com.gopay.common.domain.cust.CustCaCert;
import com.gopay.common.security.apicert.dao.CorpMsgSignCertQueryDAO;
import com.gopay.common.util.DateUtils;

/**
 * @CorpMsgSignCertQueryDAOImpl
 * @AUTHOR LIUYONG
 * @2016/9/14 16:07 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("corpMsgSignCertQueryDAO")
public class CorpMsgSignCertQueryDAOImpl extends BaseDAOImpl<CorpMsgSignCert, Serializable> implements CorpMsgSignCertQueryDAO {

    private Logger logger = LoggerFactory.getLogger(CorpMsgSignCertQueryDAOImpl.class);

    @Override
    public List<CorpMsgSignCert> queryCorpMsgSignCert(String custId) {
        String hql = "from CorpMsgSignCert where stat!=:stat and custId=:custId";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("stat","00");
        param.put("custId",custId);
        logger.info("查询语句为："+hql);
        List<CorpMsgSignCert> list = findByHql(hql, param);
        return list;
    }
    
    /**
     * 通过客户号、状态查询服务器证书列表
     * zhoutong
     * 20160913
     */
    @Override
    public List<CorpMsgSignCert> getCorpMsgSignCertList(String custId, String stat) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CorpMsgSignCert.class);
        criteria.add(Restrictions.eq("custId", custId));
        if (null != stat && !"".equals(stat.trim())) {
        	criteria.add(Restrictions.eq("stat", stat));
        }
        criteria.addOrder(Order.desc("CreateTs"));
        List<CorpMsgSignCert> result = findByCriteria(criteria);
        return result;
    }

	/**
	 * 通过主键ID获取服务器证书
	 */
	@Override
    public CorpMsgSignCert getCorpMsgSignCertById(Long id) {
        return find(CorpMsgSignCert.class, id);
	}


    @Override
    public List<CorpMsgSignCert> queryExpiringCorpMsgSignCert() {
        //查询状态是有效的过期证书
        String hql = "from CorpMsgSignCert where stat= :stat and " + "TO_CHAR(VALID_TO,'yyyy-MM-dd') <= :currentDate ";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("stat", "11");
        param.put("currentDate", DateUtils.format(new Date(), "yyyy-MM-dd"));
        logger.info("查询语句为：" + hql);
        List<CorpMsgSignCert> list = findByHql(hql, param);
        return list;

    }

}
